package com.example.oss.load;

import com.example.common.Constant;
import com.example.common.util.JacksonUtil;
import com.example.oss.storage.AliStorage;
import com.example.oss.storage.QiNiuStorage;
import com.example.oss.storage.TencentStorage;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.Map;

/**
 * <h1>oss配置文件</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020/5/9
 */
@Slf4j
@Data
@Component
public class OssLoadConfiguration implements Serializable {

    /**
     * oss配置文件前缀
     */
    private static final String PROFILE_PATH_OSS_YAML = "oss.yaml";

    /**
     * 七牛配置文件
     */
    private QiNiuStorage qiNiuStorage;

    /**
     * 阿里配置文件
     */
    private AliStorage aliStorage;

    /**
     * 腾讯配置文件
     */
    private TencentStorage tencentStorage;

    /**
     * 初始化配置文件
     */
    @PostConstruct
    public void initConfigStorage(){
        log.info("****** 初始化对象存储配置文件开始 ******");
        Yaml yaml = new Yaml();
        Object load = yaml.load(this.getClass().getClassLoader().getResourceAsStream(PROFILE_PATH_OSS_YAML));
        Map<String, Object> ossMap =  JacksonUtil.getMap(JacksonUtil.toJson(load), Object.class);
        loadingQiniuStorage(ossMap);
        loadingAliStorage(ossMap);
        loadingTencentStorage(ossMap);
        log.info("****** 初始化对象存储配置文件结束 ******");
    }


    /**
     * <h2>加载七牛云配置</h2>
     * @param ossMap
     */
    private void loadingQiniuStorage(Map<String, Object> ossMap) {
        Object qiniu = ossMap.get(Constant.CloudStorageServiceProvider.OSS_SERVICE_PROVIDER_QINIUYUN);
        this.qiNiuStorage = JacksonUtil.getObject(JacksonUtil.toJson(qiniu), QiNiuStorage.class);
    }


    /**
     * <h2>加载阿里云配置</h2>
     * @param ossMap
     */
    private void loadingAliStorage(Map<String, Object> ossMap) {
        Object ali = ossMap.get(Constant.CloudStorageServiceProvider.OSS_SERVICE_PROVIDER_ALIYUN);
        this.aliStorage = JacksonUtil.getObject(JacksonUtil.toJson(ali), AliStorage.class);
    }


    /**
     * <h2>加载腾讯云配置</h2>
     * @param ossMap
     */
    private void loadingTencentStorage(Map<String, Object> ossMap) {
        Object ali = ossMap.get(Constant.CloudStorageServiceProvider.OSS_SERVICE_PROVIDER_QCLOUD);
        this.tencentStorage = JacksonUtil.getObject(JacksonUtil.toJson(ali), TencentStorage.class);
    }
}
